from django.shortcuts import render
from django.http.response import HttpResponse


# Create your views here.


def index(request):
    return HttpResponse('ok')

def shop(request,pro_id,city_id):
    # print(pro_id,city_id)



    # import re
    # if not re.match('[0,9]', pro_id):
    #     return HttpResponse('参数不正确')


    #########查询字符串#################
    query_params = request.GET
    # print(query_params)

    # a = query_params.get('a')
    # print(a)
    # print('~~~~~~~~')
    # b = query_params['b']
    # print(b)

    #########QueryDict##################
    # 获取一键多指 getlist
    alist = query_params.getlist('a')
    print(alist)
    # 获取一键一值 get
    # asingle = query_params.get('a')
    asingle = query_params.get('aa','default_value')
    print(asingle)


    return HttpResponse('我的小餐馆')


######POST --登录##############

def login(request):
    # data = request.POST
    # print(data)
    return HttpResponse('login')

#######POST  --JSON数据#######################

def get_json(request):

    # json数据是非表单数据
    # bytes_body = request.body
    # print(bytes_body)
    #
    #
    # # 将bytes类型的数据转换为 字符串
    # body_str = bytes_body.decode()
    # print(body_str)
    #
    # # json形式的字符串
    # print(type(body_str))
    #
    # # 将字符串转换为字典
    # import json
    # data = json.loads(body_str)
    # print(data)

    return HttpResponse('json')

#######请求头########################
def get_header_info(request):

    # print(request.META)

    # print(request.META['CONTENT_TYPE'])

    data= {
        'name': 'abc'
    }
    import json
    data_str = json.dumps(data)


    return HttpResponse(data_str,content_type='application/json')


def register(request, mobile):
    print(request.method)
    return HttpResponse('注册')



from django.http.response import HttpResponse
from django.http import HttpResponse

# def http_res(request):
#     return HttpResponse('abc', status=200,content_type='text/html')


from django.http import JsonResponse

def weibo(request):

    data = [
        {
            'name': '如花',
            'age': 18
        },
        {
            'name' : '如花',
            'age' : 18
        }
    ]
    return JsonResponse(data,safe=False)



def set_cookie(request):

    username = request.GET.get('username')
    response = HttpResponse('set_cookie')

    # response.set_cookie(key='username', value='username')
    # max_age 设置过期时间
    response.set_cookie(key='username', value=username, max_age=24 * 3600)
    response.set_cookie(key='age', value=10, max_age=24 * 3600)
    response.set_cookie(key='add', value='bj', max_age=24 * 3600)
    # 删除cookie
    # response.delete_cookie()

    return response


def get_cookie(request):

    username = request.COOKIES.get('username')

    print(username)
    return HttpResponse('get_cookie')








